/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.expert.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.chlitina.store.common.config.Global;
import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.utils.CacheUtils;
import com.chlitina.store.common.web.BaseController;
import com.chlitina.store.modules.expert.entity.SolutionFaceProductStep;
import com.chlitina.store.modules.expert.service.SolutionFaceProductStepService;
import com.chlitina.store.modules.expert.utils.ExpertUtils;

/**
 *  方案产品步骤Controller
 * @author liujiang
 * @version 2013-08-19
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/expert/solutionFaceProductStep")
public class SolutionFaceProductStepController extends BaseController {

	@Autowired
	private SolutionFaceProductStepService solutionFaceProductStepService;
	
	@ModelAttribute
	public SolutionFaceProductStep get(@RequestParam(required=false) Long id) {
		if (id != null){
			return solutionFaceProductStepService.get(id);
		}else{
			return new SolutionFaceProductStep();
		}
	}

	private String getSelectPage(Page page, Long solutionId){
		Long select_stepId = (Long) CacheUtils.get("select_stepId");
		String currentId = "";
		if(select_stepId != null) {
	        int index = solutionFaceProductStepService.getIndexById(select_stepId, solutionId);
	        ExpertUtils.setPageNoByCurrentId(page, index);
	        currentId = String.valueOf(select_stepId);
	        CacheUtils.remove("select_stepId");
		}
		return currentId;
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(Long solutionId, SolutionFaceProductStep solutionFaceProductStep, HttpServletRequest request, HttpServletResponse response, Model model) {
		CacheUtils.put("solutionId", solutionId);
		Page<SolutionFaceProductStep> page = new Page<SolutionFaceProductStep>(request, response);
		String currentId = getSelectPage(page, solutionId);
		Page<SolutionFaceProductStep> current_page = solutionFaceProductStepService.find(page, solutionFaceProductStep, solutionId);
		model.addAttribute("page", current_page);
		model.addAttribute("currentId", currentId);
		model.addAttribute("solutionFaceId", solutionId);
		Map<Integer, String> orderMap = new HashMap<Integer, String>();
		for(int i = 1; i < 16; i++) {
			orderMap.put(i, "步骤"+i);
		}
		model.addAttribute("orderMap", orderMap);
		return "modules/expert/solutionFaceProductStepList";
	}
	
	@RequestMapping(value = "form")
	public String form(Long solutionId, SolutionFaceProductStep solutionFaceProductStep, Model model) {
		Map<Integer, String> orderMap = new HashMap<Integer, String>();
		for(int i = 1; i < 16; i++) {
			orderMap.put(i, "步骤"+i);
		}
		model.addAttribute("orderMap", orderMap);
		model.addAttribute("solutionFaceId", solutionId);
		model.addAttribute("solutionFaceProductStep", solutionFaceProductStep);
		CacheUtils.put("select_stepId", solutionFaceProductStep.getId());
		return "modules/expert/solutionFaceProductStepForm";
	}

	@RequestMapping(value = "save")
	public String save(SolutionFaceProductStep solutionFaceProductStep, Model model, RedirectAttributes redirectAttributes) {
		long solutionId = solutionFaceProductStep.getSolutionFace().getId();
		solutionFaceProductStepService.save(solutionFaceProductStep);
		addMessage(redirectAttributes, "保存 方案产品步骤成功");
		CacheUtils.put("select_stepId", solutionFaceProductStep.getId());
		return "redirect:"+Global.ADMIN_PATH+"/expert/solutionFaceProductStep?solutionId=" + solutionId;
	}
	
	@RequestMapping(value = "delete")
	public String delete(SolutionFaceProductStep solutionFaceProductStep, RedirectAttributes redirectAttributes) {
		long solutionId = solutionFaceProductStep.getSolutionFace().getId();
		solutionFaceProductStepService.delete(solutionFaceProductStep);
		addMessage(redirectAttributes, "删除 方案产品步骤成功");
		return "redirect:"+Global.ADMIN_PATH+"/expert/solutionFaceProductStep?solutionId=" + solutionId;
	}
	
	@ResponseBody
	@RequestMapping(value = "ifExist")
	public String ifExist(Long solutionId, int step) {
		long num = solutionFaceProductStepService.check(solutionId, step);
		if(num > 0){
			return "false";
		}
		return "true";
	}
}
